package com.gaode.gaode3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CustomMapStyleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;

import java.io.IOException;
import java.io.InputStream;


public class MainActivity extends AppCompatActivity {
    MapView mMapView = null;
    //初始化地图控制器对象
    AMap aMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        initview();
    }

    private void initview() {
        //绘制默认 Marker
        LatLng latLng = new LatLng(27.987247,119.804769);//经纬度
        // Marker    常用属性
        // position  在地图上标记位置的经纬度值。必填参数
        // title     点标记的标题
        // snippet   点标记的内容
        // draggable 点标记是否可拖拽
        // visible   点标记是否可见
        // anchor    点标记的锚点
        // alpha      点的透明度
        final Marker marker = aMap.addMarker(new MarkerOptions()
                .position(latLng)//在地图上标记位置的经纬度值
                .title("郑坑乡")//点标记的标题
                .snippet("郑坑乡"));//点标记的内容
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng)); //设置中心点
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15)); //设置缩放比例 f
        aMap.setCustomMapStyle(
                new CustomMapStyleOptions()
                        .setEnable(true)
                        .setStyleData(getAssetsStyle(MainActivity.this,"style.data"))
                        .setStyleExtraData(getAssetsStyle(MainActivity.this,"style_extra.data"))
        );
        //自定义点标记
//        MarkerOptions markerOptions=new MarkerOptions();
//        markerOptions.position(new LatLng(34,115)).title("标题").snippet("内容");
//        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.ic_launcher)));//设置图标
//        aMap.addMarker(markerOptions);


    }
    //获取libs文件
    private static byte[] getAssetsStyle(Context context,String file){
        byte[]  buffer1 = null;
        InputStream is1 = null;
        try {
            is1 = context.getResources().getAssets().open(file);
            int lenght1 = is1.available();
            buffer1 = new byte[lenght1];
            is1.read(buffer1);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (is1!=null) {
                    is1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer1;
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

}